// pages/order/detail/detail.js
import {OrderModel} from '../../../model/order'
import { config } from '../../../config'
var order = new OrderModel()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bottom:0,
    modelList:[],
    ids:"",
    allAmount:0.0,
    info:{},
    id:0,
    detail:{},
    count:0,
    address:{},
    chooseOk:{},
    flower:true,
    couponList:[],
    coupon:{}
  },

  // 选择地址
  chooseAddress(){
    var that = this
    wx.navigateTo({
      url: '/pages/address/list/list?isChoose=' + 1,
      events: {
        addressInfo: function(data) {
          that.setData({
            address:data
          })
        },
      },
    })
  },

  flowerChange:function(e){
    console.log(e)
    this.setData({
      flower: e.detail.value
    })
  },
  
  sendOrder:function(e){
    var data = this.data
    order.create(data.address.id,data.ids,data.coupon.id,data.flower?1:0).then(
      res=>{
        if (res.code===0) {
          wx.navigateTo({
            url: '/pages/order/detail/detail?id='+res.data,
          })
        }
      }
    )
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      bottom:config.statusBarHeight
    })
    var that = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('detailInfo', function(data) {
      console.log(data)
      that.setData({
        modelList: data.modelList,
        ids: data.ids,
        allAmount: data.allAmount,
      })
      // order.chooseOk(data.model.id+"").then(
      //   res=>{
      //     that.setData({
      //       chooseOk:res.data
      //     })
      //   }
      // )
      // order.conponList(data.model.id+"").then(
      //   res=>{
      //     that.setData({
      //       couponList:res.data
      //     })
      //   }
      // )
    })

    order.address().then(
      res=>{
        this.setData({
          address:res.data.address
        })
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})